import { cancel, isCancel, select } from '@clack/prompts';

import { execaCommand } from 'execa';
import { getPackages } from '../utils/packages';
import type { CAC } from 'cac';

interface RunOptions {
    command?: string;
}

export async function run(options: RunOptions) {
    const { command } = options;
    if (!command) {
        console.error('Please enter the command to run');
        process.exit(1);
    }
    const { packages } = await getPackages();
    const selectPkgs = packages.filter((pkg: any) => {
        return (pkg?.packageJson as Record<string, any>)?.scripts?.[command];
    });
    let selectPkg: symbol | string;
    if (selectPkgs.length > 1) {
        selectPkg = await select<string>({
            message: `Select the app you need to run [${command}]:`,
            options: selectPkgs.map((item: any) => ({
                label: item?.packageJson.name,
                value: item?.packageJson.name
            }))
        });

        if (isCancel(selectPkg) || !selectPkg) {
            cancel('👋 Has cancelled');
            process.exit(0);
        }
    } else {
        selectPkg = selectPkgs[0]?.packageJson?.name ?? '';
    }

    if (!selectPkg) {
        console.error('No app found');
        process.exit(1);
    }

    execaCommand(`pnpm --filter=${selectPkg as string} run ${command}`, {
        stdio: 'inherit'
    });
}

export function defineRunCommand(great: CAC) {
    great.command('run [script]').action(async (command: string) => {
        run({ command });
    });
}
